package com.sxl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxl.config.LayUiResult;
import com.sxl.domain.Askers;
import com.sxl.domain.Report;
import com.sxl.service.AskersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: CRMProject
 * @Package: com.sxl.controller
 * @Author songxueliang
 * @Date 2022/5/3 12:47
 */
@RequestMapping("/askers")
@RestController
@CrossOrigin
public class AskersController {
    @Autowired
    private AskersService askersService;

    /**
     * list查询全部     --下拉框
     * @return
     */
    @GetMapping("/selectAllListAskers")
    public List<Askers> selectAllListAskers(){
        return askersService.list();
    }

    /**
     * 页面显示 全查
     * @param page  分页
     * @param limit 偏移量
     * @param map 介绍查询参数
     * @return 返回map集合
     */
    @GetMapping("/selectAllAskers")
    public Map<String,Object> selectAllAskers(Integer page, Integer limit, @RequestParam Map<String, String> map){
        QueryWrapper<Askers> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isEmpty(map.get("askerName")) ? false : true, "askerName", map.get("askerName"))
                .orderByDesc("weight");
        Page<Map<String, Object>> pageZJ = new Page<>(page, limit);
        IPage<Map<String, Object>> rsl = askersService.pageMaps(pageZJ, wrapper);
        return LayUiResult.toClient("0","成功",rsl.getTotal(),rsl.getRecords());
    }
    /**
     * 修改权重
     * @param askers
     * @return
     */
    @PostMapping("/updateWeight")
    public Map<String,Object> updateWeight(Askers askers){
        UpdateWrapper<Askers> wrapper = new UpdateWrapper<>();
        wrapper.set("weight",askers.getWeight()).eq("askerId",askers.getAskerId());
        return askersService.update(wrapper)?LayUiResult.toClient("0","修改成功"):LayUiResult.toClient("1","修改失败");
    }
}
